package com.qianzhu8.kfc.demo.controller;

import com.qianzhu8.kfc.boot.client.KfcExecuteClient;
import com.qianzhu8.kfc.boot.props.KfcProperty;
import com.qianzhu8.kfc.boot.request.GetUserTokenRequest;
import com.qianzhu8.kfc.boot.response.GetUserTokenResponse;
import com.qianzhu8.kfc.demo.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserController
 *
 * @author xuyanjun
 * @date 2020-05-03
 */
@RestController
public class UserController {

    @Autowired
    private KfcExecuteClient kfcExecuteClient;

    @Autowired
    private KfcProperty kfcProperty;

    /**
     * 用户的登录地址
     *
     * @param userId 用户id
     * @return 登录地址
     */
    @GetMapping("/users/{userId}/loginUrl")
    public String loginUrl(@PathVariable Integer userId) {
        UserModel userModel = new UserModel();
        userModel.setId(userId)
                .setNickname("昵称")
                .setMobile("xxx");

        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest();
        getUserTokenRequest.setPlatformUniqueId(userModel.getId().toString())
                .setNickname(userModel.getNickname())
                .setMobile(userModel.getMobile());

        GetUserTokenResponse userTokenResponse = kfcExecuteClient.execute(getUserTokenRequest);
        Assert.isTrue(Boolean.TRUE.equals(userTokenResponse.getSuccess()), userTokenResponse.getMessage());

        return String.format("https://kfc.qianzhu8.com/index?token=%s&platformId=%s",
                userTokenResponse.getData().getAccessToken(), kfcProperty.getPlatformId());
    }
}
